package com.zhima;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.antgroup.zmxy.openplatform.api.DefaultZhimaClient;
import com.antgroup.zmxy.openplatform.api.ZhimaApiException;
import com.antgroup.zmxy.openplatform.api.internal.util.RSACoderUtil;
import com.antgroup.zmxy.openplatform.api.request.*;
import com.antgroup.zmxy.openplatform.api.response.ZhimaCreditIvsDetailGetResponse;
import com.antgroup.zmxy.openplatform.api.response.ZhimaCreditMobileRainGetResponse;
import com.antgroup.zmxy.openplatform.api.response.ZhimaCreditScoreGetResponse;
import com.antgroup.zmxy.openplatform.api.response.ZhimaCreditWatchlistGetResponse;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 测试各种数据查询信息的demo
 * 
 * 本处提供的demo 仅供给参考，不提供真实数�?
 * @author zmxy
 * @version $Id: TestGet.java, v 0.1 2016-1-20 下午8:51:26 Administrator Exp $
 */
public class TestGet {
    private static String serverUrl   = "https://zmopenapi.zmxy.com.cn/openapi.do";
    private static String appId       = "1000216";
    private static String charset     = "UTF-8";
    private static String zmPublicKey = "***MA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCSt7IL5JqcYEXAAaPj5YvDOSpU0qUATr8537nT65EtAXTJVLXuTjh0nBP+dTLNYJZLksDqXR45R3PPIVHtarYW6VRx1Uf7E4hd4Q5bIuexWbMzpCVuFYgybLq42nxkUCh9fX55VxuK/uLeoJ8SaHrsrG2tYVC3KOk0fPiy4/noKwIDAQAB";
    private static String privateKey  = "*****CdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJlkRQQFkpiNRULEizLtrzK5eb29905QuA1VRrktcVZxlPwTmi7SXYyivBlotlq6+CthMCFfctQswhfElL0YvVH6SMTiAzkWMHkbtbJJCppsjWTI9STd4K3tb6xzEJ3bXx9V1qJPf5hZvqWrrTZbtWCsYh6UdZSd7aXCq+TX/TkxAgMBAAECgYBhAX58CqIpA753eSCm9pXDuHR7QHXz+yxKklGFrm1sw6v6ttLdJYCv/gKC9yvLuU5U8MOld12BPUUKDvIZnFmtUaSgFWrHyAGba4HThJ9cN5lXDuHGGcMrjy+JdsvxY8TdnTy0rxYuWRZS2StjrMgwLpmTaNMnB9qmIXX22mtR2QJBAMuzOQC4NVxky/58PdmJsOOhaXFkyU9neaa4nQtQMMMhFYfV68H3CtOLkaA9TDi7kKVaNRmu6EF1bEuwMJaLID8CQQDAxmGU6A5k7m4RWFgrDD3HfqAPrJdskVv9g6dQMoVuxsg7akLgbpCRU2hY+mdTPdMZPckGUR52rFf6IGd6DEqPAkBUoRVhsyaYSFhoVn3rz0TqjbkdH15z2u2z0qeMd7iKmUg0sy/L2jNpSGcsR8tPFr41tJCzJvPfnkVr/LtzEEJJAkAqkyW2B1I2hFoWxpYiW7/BMXwUOh/vQGM6qDrpgV7qykgqpxtCCekvz2X6UMNFJSBcMMMW/A9oUkPT4Bg6YkCNAkEAtbnsr4CJO8SfxvoA56Oydllx3sDGf7Bz+VNVDSaUbljiDOjKmcE+3wI5AkFYM0cDtkXrtRUJ1XFrKX0Zgi1YjQ==";

    /**
     * 测试查询手机rain�?.
     * @throws ZhimaApiException
     */
    public static void testMobileRain() throws ZhimaApiException {
        ZhimaCreditMobileRainGetRequest creditMobileRainGetRequest = new ZhimaCreditMobileRainGetRequest();
        creditMobileRainGetRequest.setPlatform("zmop");
        creditMobileRainGetRequest.setChannel("apppc");
        //transactionId，该标记是商户每次请求的唯一标识。建议使用uuid进行传�?�，
        creditMobileRainGetRequest.setTransactionId(UUID.randomUUID().toString());
        creditMobileRainGetRequest.setProductCode("w1010100000000000105");
        creditMobileRainGetRequest.setMobile("13588159904");
        DefaultZhimaClient client = new DefaultZhimaClient(serverUrl, appId, charset, privateKey,
            zmPublicKey);
        ZhimaCreditMobileRainGetResponse getResponse = client.execute(creditMobileRainGetRequest);
        System.out.println(JSON.toJSON(getResponse));

    }

    /**
     * 测试ivs产品接口.
     * @throws ZhimaApiException 
     */
    public static void testIvsGet() throws ZhimaApiException {
        ZhimaCreditIvsDetailGetRequest creditIvsGetRequest = new ZhimaCreditIvsDetailGetRequest();
        creditIvsGetRequest.setChannel("apppc");
        creditIvsGetRequest.setPlatform("zmop");
        //transactionId，该标记是商户每次请求的唯一标识。建议使用uuid进行传�?�，
        creditIvsGetRequest.setTransactionId(UUID.randomUUID().toString());
        creditIvsGetRequest.setProductCode("w1010100000000000103");
        creditIvsGetRequest.setCertNo("330724198104241370");
        creditIvsGetRequest.setName("周晓�?");
        DefaultZhimaClient client = new DefaultZhimaClient(serverUrl, appId, charset, privateKey,
            zmPublicKey);
        ZhimaCreditIvsDetailGetResponse response = client.execute(creditIvsGetRequest);
        System.out.println(JSON.toJSON(response));

    }

    /**
     * 测试行业关注名单接口.
     * @throws ZhimaApiException
     */
    public static void testCreditWatchlist() {
        ZhimaCreditWatchlistGetRequest creditWatchlistGetRequest = new ZhimaCreditWatchlistGetRequest();
        creditWatchlistGetRequest.setPlatform("zmop");
        creditWatchlistGetRequest.setChannel("apppc");
        //transactionId，该标记是商户每次请求的唯一标识。建议使用uuid进行传�?�，
        creditWatchlistGetRequest.setTransactionId(UUID.randomUUID().toString());
        creditWatchlistGetRequest.setProductCode("w1010100100000000022");
        creditWatchlistGetRequest.setOpenId("268816113399909561399995894");
        DefaultZhimaClient client = new DefaultZhimaClient(serverUrl, appId, charset, privateKey,
            zmPublicKey);
        ZhimaCreditWatchlistGetResponse response;
        try {
            response = client.execute(creditWatchlistGetRequest);
            System.out.println(JSON.toJSON(response));
        } catch (ZhimaApiException e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试查询芝麻�?.
     * @throws ZhimaApiException 
     */
    public static void testQueryScore() throws ZhimaApiException {
        ZhimaCreditScoreGetRequest creditScoreGetRequest = new ZhimaCreditScoreGetRequest();
        creditScoreGetRequest.setPlatform("zmop");
        creditScoreGetRequest.setChannel("apppc");
        //transactionId，该标记是商户每次请求的唯一标识。建议使用uuid进行传�?�，
        creditScoreGetRequest.setTransactionId(UUID.randomUUID().toString());
        creditScoreGetRequest.setProductCode("w1010100100000000001");
        creditScoreGetRequest.setOpenId("268816113399909561399995894");
        DefaultZhimaClient client = new DefaultZhimaClient(serverUrl, appId, charset, privateKey,
            zmPublicKey);
        ZhimaCreditScoreGetResponse creditScoreGetResponse = client.execute(creditScoreGetRequest);
        System.out.println(JSON.toJSON(creditScoreGetResponse));
    }

    /**
     * 自动生成页面授权的url.
     * @throws Exception
     */
    public static void testPageAuth() throws Exception {
        ZhimaAuthInfoAuthorizeRequest authInfoAuthorizeRequest = new ZhimaAuthInfoAuthorizeRequest();
        authInfoAuthorizeRequest.setChannel("apppc");
        authInfoAuthorizeRequest.setPlatform("zmop");
        authInfoAuthorizeRequest.setIdentityType("2");
        Map<String, String> identityParams = new HashMap<String, String>();
        identityParams.put("certNo", "61042619850403354X");
        identityParams.put("name", "王曼�?");
        identityParams.put("certType", "IDENTITY_CARD");
        authInfoAuthorizeRequest.setIdentityParam(JSONObject.toJSONString(identityParams));
        //        authInfoAuthorizeRequest.setBizParams("testBizParams");
        DefaultZhimaClient client = new DefaultZhimaClient(serverUrl, appId, charset, privateKey,
            zmPublicKey);
        String pageAuthUrl = client.generatePageRedirectInvokeUrl(authInfoAuthorizeRequest);
        System.out.println(pageAuthUrl);
    }

    /**
     * 从回调地�?中获取返回�??.
     * @param url
     * @throws Exception
     */
    public static void parseFromReturnUrl(String url) throws Exception {
        int index = url.indexOf("?");
        String urlParamString = url.substring(index + 1);
        String[] paraPairs = urlParamString.split("&");
        String encryptedParam = "";
        for (String paramPair : paraPairs) {
            String[] splits = paramPair.split("=");
            if ("params".equals(splits[0])) {
                encryptedParam = splits[1];
            }
        }
        String decryptedParam = RSACoderUtil.decrypt(URLDecoder.decode(encryptedParam, charset),
            privateKey, charset);//通过浏览器返回时，不�?要decode
        System.out.println(URLDecoder.decode(decryptedParam, charset));
    }

    public static void main(String[] args) {
        try {
            testMobileRain();
        } catch (ZhimaApiException e) {
            e.printStackTrace();
        }
    }

}
